<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

  <script>

    function lengthOfLongestSubstring(s) {
      debugger
      let set = new Set();
      let left = 0;
      let maxLength = 0;

      for (let right = 0; right < s.length; right++) {
        const curr = s[right]
        while (set.has(curr)) {
          const delCurr = s[left]
          set.delete(delCurr);
          left++;
        }
        set.add(curr);
        maxLength = Math.max(maxLength, right - left + 1);
      }

      return maxLength;
    }

    // 测试代码
    console.log(lengthOfLongestSubstring('abcabcbb')); // 输出 3 ("abc")
    // console.log(lengthOfLongestSubstring('bbbbb')); // 输出 1 ("b")
    // console.log(lengthOfLongestSubstring('pwwkew')); // 输出 3 ("wke")
    // console.log(lengthOfLongestSubstring('')); // 输出 0
    // console.log(lengthOfLongestSubstring('au')); // 输出 2 ("au")

  </script>

</body>

</html>